
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Security;

namespace Blog
{
	public class BlogBizRules : Syrinx.BaseBizRules
	{
		public IBizObjList getBlogUserSettingsDbFieldNames(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			IBizObjList settingsFields = resultValue as IBizObjList;

			IBizObj blogEnabled = settingsFields.insertNew();
			blogEnabled.put("dbName", "blogEnabled");
			blogEnabled.put("bizObjName", "blogEnabled");
			blogEnabled.put("aliasName", "blogEnabled");
			blogEnabled.put("dbType", "Boolean");

			return settingsFields;
		}

		public IBizObjList getBlogUserLimitsDbFieldNames(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			IBizObjList limitFields = resultValue as IBizObjList;

			IBizObj blogEnabled = limitFields.insertNew();
			blogEnabled.put("dbName", "canEnableBlog");
			blogEnabled.put("bizObjName", "canEnableBlog");
			blogEnabled.put("aliasName", "canEnableBlog");
			blogEnabled.put("dbType", "Boolean");

			return limitFields;
		}

		public object checkForUserBlogArticleSet(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			context.put("companyId", ActiveCompanyId);
			IBizObj user = inputParams[2] as IBizObj;
			if (user != null)
			{
				string blogKey = user.KeyValue.ToString() + " Blog";
				if (true.Equals(user.get("settings.blogEnabled")))
				{
					IResults rc = app.ruleMgr.execute(context, "checkForAssociatedArticleCreateIfNeeded", "Blog", user.get("id"),
						string.Format("{0} Blog", user.get("userName")), blogKey, "Blogs");
					IBizObj article = rc.CallResults as IBizObj;
					if (article != null)
					{
						if (article.isNew)
						{
							IBizObj newPriv = app.bizObjMgr.create("AppFunction");
							newPriv.put("id", article.get("id").ToString());
							newPriv.put("privilege", (int)Privileges.Delete);
							app.ruleMgr.execute(context, "updateUserGroupAppFunctions", user.get("id"), newPriv);
						}
						else
						{
							context.put("uniqueTitle", "Blogs");
							IBizObj blogs = app.appData.get("DA.Articles.GetArticleIdByUniqueTitle", context) as IBizObj;
							if (((IComparable)article.get("parentId")).CompareTo(blogs.get("id")) != 0)
							{
								IBizObj blog = context.RuleGroup.execute(context, "getArticleDetail", blogKey, false, DetailLevel.basicDetails, false, false).CallResults as IBizObj;
								blog.put("parentId", blogs.get("id"));
								context.RuleGroup.execute(context, "updateBizObjTreeNode", blog, true);
							}
						}
					}
				}
				else
				{
					IBizObj blog = context.RuleGroup.execute(context, "getArticleDetail", blogKey, false, DetailLevel.basicDetails, false, false).CallResults as IBizObj;
					if (blog != null)
					{
						context.put("uniqueTitle", "DisabledBlogs");
						IBizObj disabledBlogs = app.appData.get("DA.Articles.GetArticleIdByUniqueTitle", context) as IBizObj;
						if (disabledBlogs != null)
						{
							blog.put("parentId", disabledBlogs.get("id"));
							context.RuleGroup.execute(context, "updateBizObjTreeNode", blog, true);
						}
					}
				}
			}
			return resultValue;
		}

		public string getBlogUserSettingsTemplate(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			if (true.Equals(ActiveCompany.get("settings.blogEnabled")) && true.Equals(CurrentUser.get("limits.canEnableBlog")))
				return Cmn.IsEmpty(resultValue) ? "Blog.Gui.AspNet.UserBlogSettingsDisplay" : (resultValue + ",Blog.Gui.AspNet.UserBlogSettingsDisplay");
			return resultValue.ToString();
		}

		public string getBlogUserLimitsTemplate(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			if (true.Equals(ActiveCompany.get("settings.blogEnabled")))
				return Cmn.IsEmpty(resultValue) ? "Blog.Gui.AspNet.UserBlogLimitsDisplay" : (resultValue + ",Blog.Gui.AspNet.UserBlogLimitsDisplay");
			return resultValue.ToString();
		}

		public IBizObjList getBlogCompanySettingsDbFieldNames(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			IBizObjList settingsFields = resultValue as IBizObjList;

			IBizObj blogEnabled = settingsFields.insertNew();
			blogEnabled.put("dbName", "blogEnabled");
			blogEnabled.put("bizObjName", "blogEnabled");
			blogEnabled.put("aliasName", "blogEnabled");
			blogEnabled.put("dbType", "Boolean");

			return settingsFields;
		}


		public string getBlogCompanySettingsTemplate(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			return resultValue + ",Blog.Gui.AspNet.CompanyBlogSettingsDisplay";
		}

		public object checkToCreateBlogArticleGroup(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			IBizObj company = inputParams[2] as IBizObj;
			if (company != null && true.Equals(company.get("settings.blogEnabled")))
			{
				IResults rc = app.ruleMgr.execute(context, "checkForAssociatedArticleCreateIfNeeded", "Blogs", company.get("id"),
					string.Format("{0} Blogs", company.get("name")),"Blogs", null);
				setupBlogSecurity(app, context, rc.CallResults as IBizObj);
				rc = app.ruleMgr.execute(context, "checkForAssociatedArticleCreateIfNeeded", "DisabledBlogs", company.get("id"),
					string.Format("{0} Disabled Blogs", company.get("name")),"DisabledBlogs", null);
				setupBlogSecurity(app, context, rc.CallResults as IBizObj);
			}
			return resultValue;
		}

		void setupBlogSecurity(IApplication app, IRuleContext context, IBizObj blogArticle)
		{
			if (blogArticle != null && blogArticle.isNew)
			{
				IBizObjList userGroups = app.ruleMgr.execute(context, "getCompanyUserGroups", ActiveCompanyId).CallResults as IBizObjList;
				IBizObj newPriv = App.bizObjMgr.create("AppFunction");
				foreach (IBizObj userGroup in userGroups)
				{
					object manageArtPriv = userGroup.get("appPrivileges.ManageArticles.privilege");
					if (manageArtPriv == null)
						manageArtPriv = userGroup.get("appPrivileges.DefaultPrivilege.privilege");
					if (!(manageArtPriv != null && manageArtPriv is int && (Privileges)manageArtPriv > Privileges.ReadOnly))
					{
						newPriv.put("id", blogArticle.get("id").ToString());
						newPriv.put("privilege", (int)Swaf.Security.Privileges.ReadOnly);
						app.ruleMgr.execute(context, "updateUserGroupAppFunctions", userGroup.get("id"), newPriv);
					}
				}
			}
		}
	}
}
